Imports System
Imports System.Collections.Specialized
Imports System.Security.Cryptography
Imports System.Text
Imports System.Web

''' -----------------------------------------------------------------------------
''' Project	 : CommonObjects
''' Class	 : itoknet.CommonObjects.Crypt
''' 
''' -----------------------------------------------------------------------------
''' <summary>
''' MD5 Encryption class designed for HTTP transmission.
''' </summary>
''' <remarks>
''' </remarks>
''' <history>
''' 	[cinj]	13/09/2004	Created
''' </history>
''' -----------------------------------------------------------------------------
Public Class Crypt
    Inherits NameValueCollection

    Public Sub New(ByVal phrase As String)
        cryptoKey = New CryptKey(phrase)
    End Sub 'New

    Public Sub New(ByVal encryptedString As String, ByVal phrase As String)
        cryptoKey = New CryptKey(phrase)
        deserialize(decrypt(HttpUtility.UrlDecode(encryptedString.Replace("_", "%"))))
    End Sub 'New

    Public ReadOnly Property EncryptedString() As String
        Get
            Return System.Web.HttpUtility.UrlEncode(encrypt(serialize())).Replace("%", "_")
        End Get
    End Property

    Public Overrides Function ToString() As String
        Return EncryptedString
    End Function 'ToString

    Private Function encrypt(ByVal serializedQueryString As String) As String
        Dim buffer As Byte() = Encoding.ASCII.GetBytes(serializedQueryString)
        Dim des As New TripleDESCryptoServiceProvider
        des.Key = Me.cryptoKey.Key
        des.IV = Me.cryptoKey.IV
        Return Convert.ToBase64String(des.CreateEncryptor().TransformFinalBlock(buffer, 0, buffer.Length))
    End Function 'encrypt

    Private Function decrypt(ByVal encryptedQueryString As String) As String
        Dim buffer As Byte() = Convert.FromBase64String(encryptedQueryString)
        Dim des As New TripleDESCryptoServiceProvider
        des.Key = Me.cryptoKey.Key
        des.IV = Me.cryptoKey.IV
        Try
            Dim t As System.Security.Cryptography.ICryptoTransform = des.CreateDecryptor()
            Return Encoding.ASCII.GetString(t.TransformFinalBlock(buffer, 0, buffer.Length))
        Catch ex As Exception
            Return ""
        End Try
    End Function 'decrypt

    Private Sub deserialize(ByVal decryptedQueryString As String)
        Dim nameValuePairs As String() = decryptedQueryString.Split("&"c)
        Dim i As Integer
        For i = 0 To nameValuePairs.Length - 1
            Dim nameValue As String() = nameValuePairs(i).Split("="c)
            If nameValue.Length = 2 Then
                MyBase.Add(nameValue(0), nameValue(1))
            End If
        Next i
    End Sub 'deserialize

    Private Function serialize() As String
        Dim sb As New StringBuilder
        Dim key As String
        For Each key In MyBase.AllKeys
            sb.Append(key)
            sb.Append("=")
            sb.Append(MyBase.Item(key))
            sb.Append("&")
        Next key
        Return sb.ToString()
    End Function 'serialize

    Private cryptoKey As CryptKey = Nothing
End Class 'SecureQueryString

